home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14451 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: jaxnet.jaxnet.com!jax!garyg
  2. From: garyg@jax.jaxnet.com (Gary M. Greenberg)
  3. Newsgroups: comp.lang.c,comp.unix.programmer
  4. Subject: Re: Q: '\n' character
  5. Followup-To: comp.lang.c,comp.unix.programmer
  6. Date: 14 Apr 1996 22:09:10 GMT
  7. Organization: Southeast Network Services, Inc.
  8. Message-ID: <4krt26$l00@jaxnet.jaxnet.com>
  9. References: <4kj66f$k0o@ren.cei.net> <1996Apr11.192937.25676@sq.com> <829396473snz@genesis.demon.co.uk>
  10. NNTP-Posting-Host: jax.jaxnet.com
  11. X-Newsreader: TIN [version 1.2 PL2]
  12.  
  13. Lawrence Kirby (fred@genesis.demon.co.uk) wrote:
  14. : In article <1996Apr11.192937.25676@sq.com> msb@sq.com "Mark Brader" writes:
  15.  
  16. : >> > Is there a function or some sort of way that I could remove '\n'
  17. : >> > charecter form the end of the string. 
  18. : >> 
  19. : >> fgets(buffer, file); 
  20.     ^^^^^^^^^^^^^^^^^^^^^^
  21. Sorry to have missed part of this thread; but doesn't fgets() require a
  22. third parameter. If buffer was declared as, say, "char buffer[BUFSIZ];"
  23. && Assuming, of course, #include <stdio.h>, then we'd have something like:
  24.  
  25.     fgets(buffer,sizeof(buffer),file);
  26.  
  27. : >> buffer[strlen(buffer)-1]=0;  
  28. : >> 
  29. : >> It may not be the fastest, but it is darned near the easiest. 
  30. : >
  31. : >Or the buggiest.  Instead use:
  32. : >
  33. : >  len = strlen (buffer);
  34. : >  if (len > 0 && buffer[len-1] == '\n') buffer[len-1] = '\0';
  35. : >
  36. : >Both tests in the "if" are necessary.
  37.  
  38. : Not exactly true. fgets() can never place a zero length string in the
  39. : buffer. However its return value can be a null pointer on end-of-file or
  40. : failure and that condition must be tested for. fgets() will leave the buffer
  41. : unchanged if it encounters end-of-file but that is the only case where
  42. : you could legally test for and possibly find a zero length string. So the
  43. : code should look something like:
  44.  
  45. Not to detract from the wisdom being imparted herein ...
  46.  
  47. :     if (fgets(buffer, file) != NULL) {
  48.      ^^^^^^^^^^^^^^^^^^^^ seems Lawrence carried the error forward.
  49.  
  50.     Is this what is meant by 'inheritance' ;-)
  51.  
  52. :         size_t len = strlen(buffer);
  53.  
  54. :         if (buffer[len-1] == '\n') buffer[len-1] = '\0';
  55. :     }
  56.  
  57. : >In the specific case of a string obtained from fgets(), we can be
  58. : >sure that if there is a newline then it is the last character.
  59. : >This leads to the alternative approach:
  60. : >
  61. : >  ptr = strchr (buffer, '\n');   /* or strrchr() */
  62. : >  if (ptr) *ptr = '\0';
  63.  
  64. : Or the very alternative approach:
  65.  
  66. :       strtok(buffer, "\n");
  67.  
  68. : But in both cases you still need to test the return code of fgets().
  69.  
  70. : Lawrence Kirby | fred@genesis.demon.co.uk 
  71.    ^^^^^            ^^^
  72.    Makes ya wonder who it is? ;-}
  73.  
  74. gary    /* the Sorcerer's Apprentice */ 
  75.     Contribute to the Randal Schwartz Legal Defense Fund
  76.        Get FREE ANSI C E-Mail Management Source Code     
  77.      Visit: http://jax.jaxnet.com/~garyg/main_page.html
  78.